﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

using GameHanhTrinh.FrameWork;

namespace GameHanhTrinh.HanhTrinh
{
    class CMenuState : CGameScreen
    {
        private CSprite _title = null;
        private List<CSprite> _menuList = new List<CSprite>();

        public CMenuState(GameStateID gameStateId, CGamePlay gamePlay)
            : base(gameStateId, gamePlay)
        {
            _RM = new CResourceManager(ResourceMgrId.RESOURCE_MENU);
        }

        public override void Init()
        {
            // Khởi tạo Resource Manager
            
            _RM.Init(CGamePlay._game.Content);
            _title = new CSprite(ResourceId.MENU_TITLE, _RM.GetTexture(ResourceId.MENU_TITLE), new Vector2(50, 10), 1, 1, 300, 100, 0);
            _menuList.Add(new CSprite(ResourceId.MENU_PLAY, _RM.GetTexture(ResourceId.MENU_PLAY), new Vector2(50, 50), 1, 1, 300, 100, 0));
            _menuList.Add(new CSprite(ResourceId.MENU_CONTINUE, _RM.GetTexture(ResourceId.MENU_CONTINUE), new Vector2(50, 50), 1, 1, 300, 100, 0));
            _menuList.Add(new CSprite(ResourceId.MENU_HIGHSCORE, _RM.GetTexture(ResourceId.MENU_HIGHSCORE), new Vector2(50, 50), 1, 1, 300, 100, 0));
            _menuList.Add(new CSprite(ResourceId.MENU_EXIT, _RM.GetTexture(ResourceId.MENU_EXIT), new Vector2(50, 150), 4, 1, 300, 100, 0));
            

            base.Init();
        }


        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, CInput input)
        {
            // Xử lý va touch
            TouchCollection touchCollection = input.ToochCollection;
            if (touchCollection.Count > 0)
            {
                if (!_isTransiting && _gamePlay._nextState == this) // Đảm bảo không bắt touch khi transition
                {
                    _gamePlay._nextState = new COptionState(GameStateID.STATE_OPTION, _gamePlay);
                    _gamePlay._nextState.TransitionOnTime = TimeSpan.FromSeconds(0.5);
                    _gamePlay._nextState.TransitionOffTime = TimeSpan.FromSeconds(0.75);
                }
            }
            base.Update(gameTime, input);

            // Tiếp theo cần xác định vị trí menu cần vẽ
            
            //float transitionOffset = 1 - (float)Math.Pow(_transitionPosition, 2); // Cần chạy từ lớn đến nhỏ nên phải ngược lại
            float transitionOffset = 1 / ((float)Math.Pow(_transitionPosition, 1)); // 0 -> 1
            Vector2 position = new Vector2(0, 100);

            for (int i = 0; i < _menuList.Count; i++)
            {
                CSprite menu = _menuList[i];
                position.X = 50;
                if (_screenState == ScreenState.TransitionOn)
                {
                    position.X += transitionOffset * 100 - 100;
                }
                else if (_screenState == ScreenState.TransitionOff)
                    position.X -= transitionOffset * 1024 - 1024;

                menu.Pos = position;
                Color color = menu.Color;
                color.A = (byte)(_transitionPosition * 255);
                //menu.Color = color;
                position.Y += menu.Height;
            }

            // Update title
            float transitionOffset2 = (float)Math.Pow(TransitionPosition, 2);
            Vector2 titlePosition = new Vector2(CGamePlay._game.GraphicsDevice.Viewport.Width/2 - _title.Width / 2, 25);
            titlePosition.Y += transitionOffset2 * 50 - 50;
            Color color2 = _title.Color;
            //Color color2 = new Color(0, 0, 0);
            color2.A = (byte)(_transitionPosition * 255);
            _title.Color = color2;
            _title.Pos = titlePosition;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (!_isVisible)
                return;
            _title.Draw(spriteBatch);
            foreach (CSprite item in _menuList)
            {
                item.Draw(spriteBatch);
            }
            base.Draw(spriteBatch);
        }
    }
}
